<?php if (!defined('NOVA_RUN')) die('You can\'t access this file directly!');

/*
-------------------------------------------------------
*
*   NovaBoard 2
*   Copyright © 2011 NovaBoard team
*
*--------------------------------------------------------
*
*   Official site: www.novaboard.net
*   Contact e-mail: support@novaboard.net
*
*   GNU General Public License, version 2:
*   http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
---------------------------------------------------------
*/

class Cache
{
	public static function check($id)
	{
		if (CACHE_MODE == true)
		{
			return file_exists(ROOT . 'cache/' . $id . '.php');
		}
		else
		{
			return false;
		}
	}

	public static function save($id, $data)
	{
		if (CACHE_MODE == true)
		{
			if (!is_dir(ROOT . 'cache'))
			{
				mkdir(ROOT . 'cache'); // Not sure on the chmod to use here
			}

			$protection = '<?php if (!defined(\'NOVA_RUN\')) die(\'You can\\\'t access this file directly!\'); ?>';
			file_put_contents(ROOT . 'cache/' . $id.'.php', $protection . "\n" . serialize($data));
		}
	}

	public static function remove($id)
	{
		if (CACHE_MODE == true)
		{
			if (file_exists(ROOT . 'cache/' . $id.'.php'))
			{
				unlink(ROOT . 'cache/' . $id.'.php');
			}
		}
	}

	// Warning! If you want to check is the cache file exist - use Cache::check instead of "if ($cach = Cach::load(...))". Empty() and same functions return true even if their arg is an empty array, so it might bring you confusing results.
	public static function load($id)
	{
		if (CACHE_MODE == true && file_exists(ROOT . 'cache/' . $id.'.php') && ($_cache = file(ROOT . 'cache/' . $id.'.php')))
		{
			return unserialize($_cache[1]);
		}
		else
		{
			return false;
		}
	}
}